#!/bin/bash

# Zenith Mind 微服务部署脚本
# 支持Windows (Git Bash) 和 Linux/MacOS

echo "=========================================="
echo "Zenith Mind 微服务 Docker 部署脚本"
echo "=========================================="

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 检查Docker是否安装
if ! command -v docker &> /dev/null; then
    echo -e "${RED}错误: Docker 未安装或未正确配置${NC}"
    exit 1
fi

# 检查Docker Compose是否安装
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
    echo -e "${RED}错误: Docker Compose 未安装${NC}"
    exit 1
fi

# 函数：显示菜单
show_menu() {
    echo ""
    echo "请选择操作:"
    echo "1) 构建所有服务"
    echo "2) 启动所有服务"
    echo "3) 停止所有服务"
    echo "4) 重启所有服务"
    echo "5) 查看服务状态"
    echo "6) 查看日志"
    echo "7) 清理所有容器和数据"
    echo "0) 退出"
    echo ""
}

# 函数：构建所有服务
build_all() {
    echo -e "${YELLOW}正在构建所有服务...${NC}"
    
    # 构建各个服务
    services=("zenith-gateway" "zenith-user-service" "zenith-api-service" "zenith-edu-service" "zenith-article-service" "zenith-middleware-service"
    
    for service in "${services[@]}"; do
        echo -e "${YELLOW}构建 $service...${NC}"
        cd "./$service"
        if [ -f "pom.xml" ]; then
            echo "正在编译 $service..."
            ./mvnw clean package -DskipTests || mvn clean package -DskipTests
            if [ $? -ne 0 ]; then
                echo -e "${RED}构建 $service 失败${NC}"
                cd ..
                return 1
            fi
        fi
        cd ..
    done
    
    echo -e "${GREEN}所有服务构建完成${NC}"
}

# 函数：启动所有服务
start_all() {
    echo -e "${YELLOW}正在启动所有服务...${NC}"
    docker-compose up -d
    
    if [ $? -eq 0 ]; then
        echo -e "${GREEN}所有服务启动成功${NC}"
        echo -e "${GREEN}等待服务启动完成...${NC}"
        sleep 30
        docker-compose ps
    else
        echo -e "${RED}服务启动失败${NC}"
    fi
}

# 函数：停止所有服务
stop_all() {
    echo -e "${YELLOW}正在停止所有服务...${NC}"
    docker-compose down
    echo -e "${GREEN}所有服务已停止${NC}"
}

# 函数：重启所有服务
restart_all() {
    echo -e "${YELLOW}正在重启所有服务...${NC}"
    docker-compose restart
    echo -e "${GREEN}所有服务已重启${NC}"
}

# 函数：查看服务状态
show_status() {
    echo -e "${GREEN}服务状态:${NC}"
    docker-compose ps
}

# 函数：查看日志
show_logs() {
    echo "选择查看日志的服务:"
    echo "1) 所有服务"
    echo "2) 网关服务 (zenith-gateway)"
    echo "3) 用户服务 (zenith-user-service)"
    echo "4) API服务 (zenith-api-service)"
    echo "5) 教育服务 (zenith-edu-service)"
    echo "6) 文章服务 (zenith-article-service)"
    echo "7) 中间件服务 (zenith-middleware-service)"
    echo "8) Nacos"
    echo "9) MySQL"
    echo "10) Redis"
    
    read -p "请输入选项 (1-8): " log_choice
    
    case $log_choice in
        1) docker-compose logs -f ;;
        2) docker-compose logs -f zenith-gateway ;;
        3) docker-compose logs -f zenith-user-service ;;
        4) docker-compose logs -f zenith-api-service ;;
        5) docker-compose logs -f zenith-edu-service ;;
        6) docker-compose logs -f zenith-article-service ;;
        7) docker-compose logs -f zenith-middleware-service ;;
        8) docker-compose logs -f nacos ;;
        9) docker-compose logs -f mysql ;;
        10) docker-compose logs -f redis ;;
        *) echo "无效选项" ;;
    esac
}

# 函数：清理所有容器和数据
clean_all() {
    echo -e "${RED}警告: 这将删除所有容器和数据卷${NC}"
    read -p "确定要继续吗? (y/N): " confirm
    
    if [[ $confirm == [yY] || $confirm == [yY][eE][sS] ]]; then
        echo -e "${YELLOW}正在清理所有容器和数据...${NC}"
        docker-compose down -v
        docker system prune -f
        echo -e "${GREEN}清理完成${NC}"
    else
        echo "取消清理操作"
    fi
}

# 主循环
while true; do
    show_menu
    read -p "请输入选项 (0-7): " choice
    
    case $choice in
        1) build_all ;;
        2) start_all ;;
        3) stop_all ;;
        4) restart_all ;;
        5) show_status ;;
        6) show_logs ;;
        7) clean_all ;;
        0) echo "退出脚本"; exit 0 ;;
        *) echo -e "${RED}无效选项，请重新输入${NC}" ;;
    esac
done